package com.example.duoshujuyuan.Controller;

import com.example.duoshujuyuan.entity.UserEntity;
import com.example.duoshujuyuan.service.Interface.IDataSourceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/demo")
public class TestController {

    @Autowired
    private IDataSourceService DataSourceService;

    /**
     * 测试web正常启动
     * @return
     */
    @RequestMapping(value = "/test",method = RequestMethod.GET)
    public String helloreq(){
        return "helloword!Durid!++";
    }

    /**
     * 请求第一个数据库的数据
     * @param id
     * @return
     */
    @RequestMapping(value = "/one/{id}",method = RequestMethod.GET)
    public UserEntity testOneDataSource(@PathVariable Integer id){
        System.out.println("id:" + id);
        return DataSourceService.getUserByIdOneDataSource(id);
    }

    /**
     * 请求第二个数据库的数据
     * @param id
     * @return
     */
    @RequestMapping(value = "/two/{id}",method = RequestMethod.GET)
    public UserEntity testTwoDataSource(@PathVariable Integer id){
        System.out.println("id:" + id);
        return DataSourceService.getUserByIdtwoDataSource(id);
    }

    /**
     * 测试分页
     * @param user
     * @return
     */
    @RequestMapping(value = "/fenYe",method = RequestMethod.GET)
    public PageInfo<UserEntity> testFenYe(UserEntity user){
        //PageHelper分页方法，传入的是当前页及每页显示条数
        PageHelper.startPage(2, 2);
        List<UserEntity> userList = DataSourceService.getUserList(user);
        //PageInfo其实是pagehelper给我们封装的一个类，里面有一些我们做分页表常用的属性
        PageInfo<UserEntity> studentPageInfo = new PageInfo<>(userList);
        return studentPageInfo;
    }

}
